package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjContractDao;
import cn.topevery.garden.entity.dto.ProjMoneyDTO;
import cn.topevery.garden.entity.po.ProjContract;
import cn.topevery.garden.entity.query.ProjContractQuery;
import cn.topevery.garden.entity.vo.ProjContractMoneyVO;
import cn.topevery.garden.entity.vo.ProjContractVO;
import cn.topevery.garden.entity.vo.ProjSituationRecordMoneyVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * 项目合同表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Repository
public class ProjContractDaoImpl extends BaseDaoImpl<ProjContract> implements ProjContractDao {
    @Override
    public <P extends DbCsp> List<ProjContract> getSearch(P para) {
        return null;
    }

    public SqlQueryMate getSqlQueryMate(ProjContractQuery query) {
        String table = "t_proj_contract tpc";
        String columns = "tpc.*";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tpc.c_db_create_date asc");
        sqlQueryMate.where(true, "tpc.c_db_status", ScriptConditionEnum.Eq, 0);
        sqlQueryMate.where(true, "tpc.c_proj_id", ScriptConditionEnum.Eq, query.getProjId());
        return sqlQueryMate;
    }

    @Override
    public List<ProjContractVO> list(ProjContractQuery query) {
        SqlQueryMate sqlQueryMate = getSqlQueryMate(query);
        return selectList(sqlQueryMate, ProjContractVO.class);
    }

    @Override
    public DataPager<ProjContractVO> listPage(ProjContractQuery query) {
        Integer pageIndex = query.pageIndex - 1;
        SqlQueryMate sqlQueryMate = getSqlQueryMate(query);
        return getDataPager(sqlQueryMate, query.pageSize, pageIndex, ProjContractVO.class);
    }

    @Override
    public void saveOrUpdate(String userId, Class<ProjContract> entityClass, ProjContract entity) {

    }

    @Override
    public void saveOrUpdate(String userId, Class<ProjContract> entityClass, Collection<ProjContract> list) {

    }

    @Override
    public ProjContract getEntityByGuidId(String contractId) {
        return getEntityByColumnEqValue(new KeyValue<>("c_contract_id", contractId));
    }

    @Override
    public ProjContractMoneyVO getContractMoney(ProjMoneyDTO moneyDTO) {
        String table = "t_proj_contract tpc";

        String columns = " sum(tpc.c_contract_money) c_contract_money, " +
                "sum(tpc.c_approved_money) c_approved_money";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tpc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(ObjectUtil.isNotEmpty(moneyDTO.getProjId()), "tpc.c_proj_id", ScriptConditionEnum.Eq, moneyDTO.getProjId())
        ;
        return selectOne(sqlQueryMate, ProjContractMoneyVO.class);
    }
}
